package ch.reevolt.curchod.ui;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ConfirmationPane extends JFrame {

	private static ConfirmationPane _instance = new ConfirmationPane();

	/**
	 * 
	 */
	private static final long serialVersionUID = -8324307065459192912L;

	Image icon_tick;
	Image icon_error;
	Image icon;
	JLabel icon_surface;
	JPanel panel;

	private ConfirmationPane() {

		panel = new JPanel();

		setUndecorated(true);
		
		try {
			icon_tick = ImageIO.read(MainFrame.class.getResource("/images/tick.png"));
			icon_error = ImageIO.read(MainFrame.class.getResource("/images/error.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		icon_surface = new JLabel();
		panel.add(icon_surface);
		add(panel);

		// Get the size of the screen
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

		setSize(450, 450);

		// Determine the new location of the window
		int w = getSize().width;
		int h = getSize().height;
		int x = (dim.width - w) / 2;
		int y = (dim.height - h) / 2;

		// Move the window
		setLocation(x, y);

	}

	public void display(boolean state) {
		
		panel.remove(icon_surface);

		if (state)
			icon_surface = new JLabel( new ImageIcon(icon_tick));
		else
			icon_surface = new JLabel( new ImageIcon(icon_error));

		panel.add(icon_surface);
		setVisible(true);

		new Autoclose();
	}

	private class Autoclose extends Timer {
		public Autoclose() {
			schedule(new TimerTask() {

				@Override
				public void run() {
					ConfirmationPane.getInstance().close();
				}
			}, 1500);
		}
	}

	public static ConfirmationPane getInstance() {
		return _instance;
	}

	public void close() {
		this.setVisible(false);
		this.dispose();
	}

}
